package net.neoremind.fountain.producer.parser.impl;

import java.nio.ByteBuffer;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;

import net.neoremind.fountain.event.ColumnDataParser;
import net.neoremind.fountain.event.data.ColumnData;
import net.neoremind.fountain.meta.ColumnMeta;
import net.neoremind.fountain.util.UnsignedNumberHelper;

public class DateTimeColumnDataParser implements ColumnDataParser {

	@Override
	public void parse(ByteBuffer buf, ColumnData columnData, ColumnMeta meta) {
		long dateTimeLong = UnsignedNumberHelper.convertLittleEndianLong(buf, 8);
		int date = (int)(dateTimeLong / 1000000);
        int time = (int)(dateTimeLong % 1000000);
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(date / 10000, (date % 10000) / 100 -1, date % 100, 
                time / 10000, (time % 10000) / 100, time % 100);
        
        columnData.setValue(new Timestamp(cal.getTimeInMillis()));
    	columnData.setJavaType(Types.TIMESTAMP);
    	columnData.setSqlType(meta.getTypeEnum().getTypeValue());
	}

}
